package com.jshoperxms.service.impl;

import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.dao.CouponCodeTDao;
import com.jshoperxms.dao.CouponTDao;
import com.jshoperxms.entity.CouponCodeT;
import com.jshoperxms.entity.CouponT;
import com.jshoperxms.service.CouponTService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.recycler.Recycler;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sdywcd on 17/1/10.
 * Des:
 */

@Service("couponTService")
@Scope("prototype")
public class CouponTServiceImpl extends BaseTServiceImpl<CouponT> implements CouponTService {


    @Resource
    private CouponTDao couponTDao;

    @Resource
    private CouponCodeTDao couponCodeTDao;




    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void delCouponTransactional(CouponT rq) {
        String ids[]= StringUtils.split(rq.getId(), TagWords.SPLITDOT);
        for(String s:ids){
            CouponT bean=this.couponTDao.findByPK(CouponT.class,s);
            if(bean!=null){
                bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                this.couponTDao.update(bean);
                Criterion criterion= Restrictions.eq("couponid",s);
                List<CouponCodeT> list=this.couponCodeTDao.findByCriteria(CouponCodeT.class,criterion);
                for(CouponCodeT c:list){
                    c.setStatus(BaseEnums.DataUsingState.DEL.getState());
                    c.setUpdatetime(DateTools.getSystemTimeBi());
                    c.setVersiont(c.getVersiont()+1);
                    this.couponCodeTDao.update(c);
                }
            }
        }
    }
}
